package com.yuhanginfo.modules.web.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.entity.Notice;
import com.yuhanginfo.modules.web.entity.NoticeType;
import com.yuhanginfo.modules.web.service.NoticeService;
import com.yuhanginfo.modules.web.service.WebClassService;

/**
 * 公告
 *
 */
@Controller
@RequestMapping(value = "${frontPath}/notice")
public class NoticeController extends BaseController{

	@Autowired
	private NoticeService service;
	@Autowired
	private WebClassService webClassService;
	
	@RequestMapping(value = {"", ""})
	public String getInfo(Map<String, String> params,HttpServletRequest request, HttpServletResponse response, Model model) {
		List<NoticeType> typeList=service.findAllType();
		model.addAttribute("typeList",typeList);
		Notice notice = new Notice();
		if(StringUtils.isBlank(params.get("noticeType"))) {
			params.put("noticeType", typeList.get(0).getId());
			notice.setType(typeList.get(0).getId());
		}else{
			notice.setType(params.get("noticeType"));
		}
		Page<Notice> page = service.findPage(new Page<Notice>(request, response), notice);
		model.addAttribute("page",page);
		model.addAttribute("params",params);
		model.addAttribute("allClassesList",webClassService.getAllClasses());
		return "modules/web/notice";
	}
	
	
	@RequestMapping(value = "findNoticeByType")
	public String findNoticeByType(String noticeType,HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, String> params=new HashMap<String, String>();
		if(StringUtils.isNotBlank(noticeType)) {
			params.put("noticeType", noticeType);
		}
		return getInfo(params,request,response, model);
	}
	
	@RequestMapping(value = "noticeDetail")
	public String noticeDetail(String id,String noticeType,HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, String> params=new HashMap<String, String>();
		if(StringUtils.isNotBlank(id)) {
			params.put("id", id);
		}
		if(StringUtils.isNotBlank(noticeType)) {
			params.put("noticeType", noticeType);
		}
		return getInfo(params,request,response, model);
	}
	
	
}
